package com.houserent.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.houserent.dao.LogDao;
import com.houserent.entry.HouseInfo;
import com.houserent.entry.HouseResult;
import com.houserent.entry.LogInfo;
import com.houserent.utils.HouseUtils;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    @Resource
    private LogDao logDao;

    // 登录
    @Override
    public HouseResult logIn(String userName, String password) {
        LogInfo user = logDao.login(userName);
        if (HouseUtils.isEmpty(user) || !user.getPass_word().equals(password)) {
            return new HouseResult(HouseResult.ERROR_CODE, "用户名或密码错误", "");
        }
        return new HouseResult(HouseResult.SUCCESS_CODE, "登录成功", user);
    }

    // 注册
    @Override
    public HouseResult register(String userName, String password, String telPhone, String userType) {
        if (!HouseUtils.isEmpty(logDao.login(userName))) {
            return new HouseResult(HouseResult.ERROR_CODE, "该账号已被注册", "");
        }
        LogInfo user = new LogInfo();
        user.setUser_id(userName);
        user.setPass_word(password);
        user.setLink_phone(telPhone);
        user.setUser_type("00");// 默认房主
        user.setIs_full("00");// 未完善信息
        logDao.register(user);
        return new HouseResult(HouseResult.SUCCESS_CODE, "注册成功,请牢记密码", "");
    }

    @Override
    public HouseResult modifyUserInfo(String userId, String linkPhone, String realName, String sex, String certNum,
            String email, String qq, String fullTag) {
        // 修改个人信息
        if (HouseUtils.isEmpty(userId)) {
            return new HouseResult(HouseResult.ERROR_CODE, "未获取到要修改的用户账号,请重试", "");
        }
        LogInfo user = logDao.login(userId);
        if (HouseUtils.isEmpty(user)) {
            return new HouseResult(HouseResult.ERROR_CODE, "未获取到要修改的用户账号,请重试", "");
        }
        if (!HouseUtils.isEmpty(linkPhone)) {
            user.setLink_phone(linkPhone);
        }
        if (!HouseUtils.isEmpty(realName)) {
            user.setReal_name(realName);
        }
        if (!HouseUtils.isEmpty(sex)) {
            user.setSex(sex);
        }
        if (!HouseUtils.isEmpty(certNum)) {
            user.setCert_num(certNum);
        }
        if (!HouseUtils.isEmpty(email)) {
            user.setE_mail(email);
        }
        if (!HouseUtils.isEmpty(qq)) {
            user.setQq(qq);
        }
        fullTag = user.getIs_full();
        if ("00".equals(fullTag)) {// 00状态下为非完善信息,要先完善个人信息
            if (HouseUtils.isEmpty(realName, sex, certNum, email, qq)) {
                return new HouseResult(HouseResult.ERROR_CODE, "完善个人信息时不允许填写空内容!", "");
            }
            user.setIs_full("01");// 完善的个人信息
        }
        logDao.fullUserInfo(user);
        return new HouseResult(HouseResult.SUCCESS_CODE, "修改成功", "");
    }

    @Override
    public HouseResult modifypwd(String userId, String oldPwd, String newPwd) {
        if (HouseUtils.isEmpty(userId)) {
            return new HouseResult(HouseResult.ERROR_CODE, "未获取到要修改的用户账号,请重试", "");
        }
        LogInfo user = logDao.login(userId);
        if (!user.getPass_word().equals(oldPwd)) {
            return new HouseResult(HouseResult.ERROR_CODE, "旧密码输入错误!", "");
        }
        if (!oldPwd.equals(newPwd)) {
            user = new LogInfo();
            user.setUser_id(userId);
            user.setPass_word(newPwd);
            logDao.fullUserInfo(user);
        }
        return new HouseResult(HouseResult.SUCCESS_CODE, "密码修改成功,请牢记新密码", "");
    }

    @Override
    public HouseResult qryAllUserByhostId(String hostId) {
        // 获取房主下的所有用户
        List<HouseInfo> useHouses = logDao.qryAllUserByhostId(hostId);
        if (HouseUtils.isEmpty(useHouses)) {
            return new HouseResult(HouseResult.ERROR_CODE, "暂无住户", "");
        }
        return new HouseResult(HouseResult.SUCCESS_CODE, "获取成功", useHouses);
    }

}
